package com.itheima.controller.store;

import com.github.pagehelper.PageInfo;
import com.itheima.controller.BaseServlet;
import com.itheima.domain.store.Orders;
import com.itheima.domain.store.Product;
import com.itheima.service.impl.store.ProductServiceImpl;
import com.itheima.service.store.ProductService;
import com.itheima.utils.BeanUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/store/product/*")
public class ProductServlet extends BaseServlet {

    private ProductService productService=new ProductServiceImpl();
    //分页
    protected void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int pageNum = 1;
        int pageSize = 3;
        if (req.getParameter("pageNum") != null){
            pageNum = Integer.parseInt(req.getParameter("pageNum"));}
        if (req.getParameter("pageSize") != null){
            pageSize = Integer.parseInt(req.getParameter("pageSize"));}
        List<Product> list=productService.list(pageNum,pageSize);
        PageInfo pageInfo=new PageInfo(list);
        req.setAttribute("pageInfo",pageInfo);
        req.setAttribute("methodList","list");
        req.getRequestDispatcher("/WEB-INF/pages/store/product/product-list.jsp").forward(req,resp);
    }

    //跳转增加
    protected void toAdd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher("/WEB-INF/pages/store/product/product-add.jsp").forward(req,resp);
    }

    //新增
    protected void save(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Product product = BeanUtil.fillBean(req, Product.class,"yyyy-MM-dd");
        //传给业务
        productService.save(product);
        list(req,resp);
    }


    //3、点击删除按钮，实现删除复选框选中数据
    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String ids = req.getParameter("ids");
        String[] split = ids.split(",");
//        System.out.println(split);
        for (int i = 0; i < split.length; i++) {
            productService.deleteById(split[i]);
        }
        list(req, resp);
    }

    //回显
    protected void toEdit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        Product product= productService.findById(id);
        //查询已有的出发城市
        List<String> cityList=productService.findCity();
        req.setAttribute("product",product);
        req.setAttribute("cityList",cityList);
        req.getRequestDispatcher("/WEB-INF/pages/store/product/product-update.jsp").forward(req,resp);
    }

    //修改
    protected void edit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Product product = BeanUtil.fillBean(req, Product.class);
        productService.edit(product);
        list(req, resp);
    }

    //改状态,开启
    protected void toon(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String ids = req.getParameter("ids");
        String[] split = ids.split(",");
        for (int i = 0; i < split.length; i++) {
            productService.toon(split[i]);
        }
        list(req, resp);
    }

    //改状态,关闭
    protected void tooff(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String ids = req.getParameter("ids");
        String[] split = ids.split(",");
        for (int i = 0; i < split.length; i++) {
            productService.tooff(split[i]);
        }

        list(req, resp);
    }

    private String msg;
    //模糊查询
    protected void selectLike(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int pageNum = 1;
        int pageSize = 3;
        if (req.getParameter("pageNum") != null)
            pageNum = Integer.parseInt(req.getParameter("pageNum"));
        if (req.getParameter("pageSize") != null)
            pageSize = Integer.parseInt(req.getParameter("pageSize"));
        if (req.getParameter("msg")!=null){
            msg = req.getParameter("msg");
        }
        String lms="%"+msg+"%";
        List<Product> productList= productService.findByLike(lms,pageNum,pageSize);
        PageInfo pageInfo=new PageInfo(productList);
        req.setAttribute("methodList","selectLike");
        req.setAttribute("pageInfo",pageInfo);
        req.getRequestDispatcher("/WEB-INF/pages/store/product/product-list.jsp?pageNum="+pageNum).forward(req,resp);
    }

    //异步更改
    protected void selectStatus(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        String id = req.getParameter("id");
        //传id获得product对象
        Product product= productService.selectStatus(id);
        Integer productStatus = product.getProductStatus();
//        req.setAttribute("status",status);
        if (productStatus == 1){
            product.setProductStatus(0);
//            resp.getWriter().write("已禁用");
        }else{
            product.setProductStatus(1);
//            resp.getWriter().write("启用中");
        }
        //修改
        productService.edit(product);
        resp.setContentType("text/html;charset=utf-8");
        resp.getWriter().write(""+product.getProductStatus());
    }
}
